package it.canraid.leaflet.view;

import it.canraid.leaflet.control.Kernel;
import it.canraid.leaflet.control.activity.DrawActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.Transformation;
import android.widget.RelativeLayout;

public class FlingPanel extends RelativeLayout {
    private Animation a = null;
    private DrawActivity context;

    private volatile boolean isFirstView = true;
    private int mContentWidth = 0;
    private int actualId = -1;

    public FlingPanel(DrawActivity context) {
	super(context);
	this.context = context;

	RelativeLayout.LayoutParams fLP1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
	fLP1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
	fLP1.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
	fLP1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 1);
	addView(this.context.drawBody, fLP1);
	RelativeLayout.LayoutParams fLP2 = new RelativeLayout.LayoutParams(0, RelativeLayout.LayoutParams.MATCH_PARENT);
	fLP2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
	fLP2.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
	fLP2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 1);
	addView(this.context.textBody, fLP2);

	setEnabled(true);
	actualId = this.context.drawBody.getId();
	context.leftButton.setVisibility(View.INVISIBLE);
	context.rightButton.setVisibility(View.VISIBLE);
	context.leftButton.setBackgroundColor(Kernel.COLOR_DRAW_TEXT_1);
	this.context.drawBody.setColor(Kernel.COLOR_DRAW_TEXT_1);
	context.rightButton.setBackgroundColor(Kernel.COLOR_DRAW_TEXT_2);
	this.context.textBody.setColor(Kernel.COLOR_DRAW_TEXT_2);

    }

    public int getActualId() {
	return actualId;
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	if (mContentWidth == 0) {
	    this.context.drawBody.measure(heightMeasureSpec, MeasureSpec.UNSPECIFIED);
	    mContentWidth = this.context.drawBody.getMeasuredWidth();
	}
	super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void getFirstView() {
	if (isFirstView)
	    return;

	a = new FlingAnimation(0, 1, true, this.context.drawBody.getId());
	a.setDuration(Kernel.TIME_ANIMATION_FLING_PANEL);
	startAnimation(a);
    }

    public void getSecondView() {
	if (!isFirstView)
	    return;

	a = new FlingAnimation(1, 0, false, this.context.textBody.getId());
	a.setDuration(Kernel.TIME_ANIMATION_FLING_PANEL);
	startAnimation(a);
    }

    private class FlingAnimation extends Animation implements AnimationListener {
	private final int firstViewStart;
	private final int viewDelta;
	private final boolean expectedIsFirstView;
	private final int expectedActualId;

	public FlingAnimation(int percFirstViewStart, int percFirstViewEnd, boolean expectedIsFirstView, int expectedActualId) {
	    this.expectedIsFirstView = expectedIsFirstView;
	    firstViewStart = (mContentWidth * percFirstViewStart);
	    viewDelta = mContentWidth * (percFirstViewEnd - percFirstViewStart);
	    setAnimationListener(this);
	    this.expectedActualId = expectedActualId;
	}

	protected void applyTransformation(float interpolatedTime, Transformation t) {
	    int w1 = (int) (firstViewStart + viewDelta * interpolatedTime);
	    int w2 = mContentWidth - w1;

	    android.view.ViewGroup.LayoutParams lp1 = context.drawBody.getLayoutParams();
	    android.view.ViewGroup.LayoutParams lp2 = context.textBody.getLayoutParams();

	    lp1.width = w1;
	    lp2.width = w2;

	    context.drawBody.setLayoutParams(lp1);
	    context.textBody.setLayoutParams(lp2);
	}

	public boolean willChangeBounds() {
	    return true;
	}

	public void onAnimationEnd(Animation animation) {
	    isFirstView = expectedIsFirstView;
	    actualId = expectedActualId;

	    if (isFirstView) {
		context.drawBody.setEnabled(true);
		context.textBody.setEnabled(false);
		context.leftButton.setVisibility(View.INVISIBLE);
		context.rightButton.setVisibility(View.VISIBLE);
	    } else {
		context.drawBody.setEnabled(false);
		context.textBody.setEnabled(true);
		context.leftButton.setVisibility(View.VISIBLE);
		context.rightButton.setVisibility(View.INVISIBLE);
	    }
	}

	public void onAnimationRepeat(Animation animation) {
	}

	public void onAnimationStart(Animation animation) {
	}

    }
}